function doHighlight(bodyText, searchTerm) 
{
	highlightStartTag = "<font class=searchlite>";
	highlightEndTag = "</font>";
  
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


function highlightSearchTerms(searchText, treatAsPhrase, searchArea)
{
	for (var i=0;i<searchText.length;i++){
		if ((searchText.charAt(i)+searchText.charAt(i+1)).toUpperCase()=='OR'){
			treatAsPhrase = false;
		}
		if ((searchText.charAt(i)+searchText.charAt(i+1)+searchText.charAt(i+2)).toUpperCase()=='AND'){
			treatAsPhrase = false;
		}
	}
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if(document.getElementById(searchArea))
  {
	  var bodyText = document.getElementById(searchArea).innerHTML;
  }
  else
  {
		var bodyText = ""; 
  }
  for (var i = 0; i < searchArray.length; i++) {
	if (searchArray[i].toUpperCase() !='OR' && searchArray[i].toUpperCase()!='AND'){
		if (searchArray[i].charAt(0)=='"'){
			searchArray[i]=searchArray[i].substring(1,searchArray[i].length)
		}
		if (searchArray[i].charAt(searchArray[i].length-1)=='"'){
			searchArray[i]=searchArray[i].substring(0,searchArray[i].length-1)
		}
		bodyText = doHighlight(bodyText, searchArray[i]);
	}
  }
  
  if(bodyText)
  {	
  	document.getElementById(searchArea).innerHTML = bodyText;
  }
  return true;
}



function LTrim(iStr)
{
while (iStr.charCodeAt(0) <= 32)
{
iStr=iStr.substr(1);
}
return iStr;
}

function RTrim(iStr)
{
while (iStr.charCodeAt(iStr.length - 1) <= 32)
{
iStr=iStr.substr(0, iStr.length - 1);
}
return iStr;
}

function Trim(iStr)
{
while (iStr.charCodeAt(0) <= 32)
{
iStr=iStr.substr(1);
}

while (iStr.charCodeAt(iStr.length - 1) <= 32)
{
iStr=iStr.substr(0, iStr.length - 1);
}

return iStr;
}
function UnicodeGet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; )
	{
		if (iStr.charCodeAt(i)==38)
		{
			if (iStr.charCodeAt(i + 1)==35)
			{
				p=iStr.indexOf(';', i  + 2);
				if (p!=-1)
				{
					if (p - i <= 7)
					{
						if (isFinite(iStr.substr(i + 2, p - i - 2)))
						{
							oStr = oStr.concat(String.fromCharCode(iStr.substr(i + 2, p - i - 2)));
							i = p + 1;
							continue;
						}
					}
				}
			}
			else
			{
				p=iStr.indexOf(';', i  + 1);
				if (p!=-1)
				{
					switch (iStr.substr(i + 1, p - i - 1))
					{
					case 'amp':
						oStr = oStr.concat('&');
						i = p + 1;
						break;
					case 'quot':
						oStr = oStr.concat('"');
						i = p + 1;
						break;
					case 'lt':
						oStr = oStr.concat('<');
						i = p + 1;
						break;
					case 'gt':
						oStr = oStr.concat('>');
						i = p + 1;
						break;
					}
				}
			}
		}
	
	
		oStr=oStr.concat(iStr.charAt(i));
		i++;
	}
	
	return oStr;
}
function searchPrompt(defaultText, treatAsPhrase, searchArea)
{ 
	defaultText = Trim(defaultText);
	if (defaultText=='') return;
  return highlightSearchTerms(UnicodeGet(defaultText), treatAsPhrase, searchArea);
}
